/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.mwas.proteana.math.factorials;

import java.math.BigDecimal;
import java.util.HashMap;

/**
 *
 * @author loskov
 */
public class FactorialFactory {

    private static HashMap<BigDecimal, Factorial> resultCache;

    public FactorialFactory() {
        if(resultCache == null) {
            resultCache = new HashMap<>();
        }
    }

    public Factorial getFactorial(int n) {
        return getFactorial(new BigDecimal(n));
    }

    public Factorial getFactorial(BigDecimal n) {
        if (isCached(n)) {
            return resultCache.get(n);
        } else {
            Factorial instance;
            if (n.intValue() < 200) {
                instance = new FactorialSimple(n);
            } else {
                instance = new FactorialPochammer(n);
            }
            resultCache.put(n, instance);
            return instance;
        }
    }

    public boolean isCached(int index) {
        return isCached(new BigDecimal(index));
    }
    
    public boolean isCached(BigDecimal index) {
        return resultCache.containsKey(index);
    }

    @Override
    public String toString() {
        return resultCache.toString();
    }
}
